import axios from 'axios';
import { localstorage } from "@/utils"
import { ACCESS_TOKEN, BEARER, HOST } from "@/constant"


// axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';

const token = () => `${BEARER} ${localstorage.getLocalStorage(ACCESS_TOKEN)}`;

const host = HOST;
const client = axios.create({
  baseURL: host,
  timeout: 5000,
  withCredentials: true, // 允许携带跨域cookie
  headers: {
    Authorization: `${localstorage.getLocalStorage(ACCESS_TOKEN) ? token() : ""}`,
  }
});

let cancelTokenSource:any;

client.interceptors.request.use(
  config => {
     config.headers['Content-Type'] = 'application/json';

     if(localstorage.getLocalStorage(ACCESS_TOKEN)){
 config.headers['Authorization'] = `${BEARER} ${localstorage.getLocalStorage(ACCESS_TOKEN)}`
     }
    

    // 设置其他通用头部

    // 如果有正在进行的请求，取消它
    if (cancelTokenSource) {
      cancelTokenSource?.cancel('Operation canceled by the user.');
    }

    // 创建新的CancelToken
    cancelTokenSource = axios.CancelToken.source();
    config.cancelToken = cancelTokenSource.token;

    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

client.interceptors.response.use(
  response => {
    return response.data;
  },
  async error => {
    if (axios.isCancel(error)) {
      console.log('Request canceled:', error.message);
      // 可以在这里处理请求被取消的逻辑
    } else if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = '请求错误';
          break;
        case 401:
          error.message = '未授权，请登录';
          break;
        // 其他错误处理
        default:
          error.message = '服务端异常，请联系管理员';
      }
    }
    return Promise.reject(error);
  }
);

interface ApiParams{
  url: string
  data?: any
  params?: any
  config?: {
    [key in any] : any
  }
}

type ApiMethod = (params: ApiParams) => Promise<any>


interface ApiProps {
  get: ApiMethod
  post: ApiMethod
  put: ApiMethod
  delete: ApiMethod
}


// 封装通用的GET、POST、PUT、DELETE方法，并添加请求重试
const api : ApiProps = {
  get: ({url, params, config}) => client.get(url, { params, ...config }),
  post: ({url, data, config}) => client.post(url, data, config),
  put: ({url, data, config}) => client.put(url, data, config),
  delete: ({url, data, config}) => client.delete(url, { data, ...config }),
}

export { api, client };
